package servlets;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.User;

import queries.DBConnector;
import queries.SearchQueries;
import queries.SecurityQueries;
import queries.UserManagementQueries;

/**
 * Servlet implementation class Image
 */
@WebServlet("/Image")
public class Image extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Image() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);		
		
		DBConnector dbConnect = new DBConnector();
		
		try {
			String record_id = (request.getParameter("record_id")).trim();
			String image_id = (request.getParameter("image_id")).trim();
			String size = (request.getParameter("size")).trim();			
			int r_id = Integer.parseInt(record_id);
			int i_id = Integer.parseInt(image_id);
			
			String query = "SELECT "+ size +" FROM pacs_images WHERE image_id = "+ image_id +" AND record_id = "+ record_id;

			Statement st = dbConnect.getConnection().createStatement();
			
			ResultSet rset = st.executeQuery(query);
			String imgLen="";
			if(rset.next()) {
				imgLen = rset.getString(1);
			}	
			
			rset = st.executeQuery(query);
			
			if(rset.next()) {
				int len = imgLen.length();
				byte [] rb = new byte[len];
				InputStream readImg = rset.getBinaryStream(1);
				int index = readImg.read(rb, 0, len);
				response.reset();
				response.setContentType("image/jpg");
				response.setHeader("Content-disposition","attachment; filename=test");
				response.getOutputStream().write(rb,0,len);
				response.getOutputStream().flush();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dbConnect.getConnection().close();
			} catch (SQLException e){
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
